﻿using System;
using System.Collections.Generic;
using System.Web;

namespace Gammar.Digger.SystemFramework
{
    /// <summary>
    /// Cookie相关操作
    /// </summary>
    public class CookieHelper
    {
        #region CookieHelper
        /// <summary>
        /// 获取Cookie
        /// </summary>
        /// <param name="name">Cookie Name</param>
        /// <returns></returns>
        public static string GetCookie(string name)
        {
            HttpRequest request = HttpContext.Current.Request;
            if (request.Cookies.Get(name) != null)
                return HttpUtility.UrlDecode(request.Cookies[name].Value);
            else
                return null;
        }
        /// <summary>
        /// 根据HttpRequest获取Cookie
        /// </summary>
        /// <param name="name">Cookie Name</param>
        /// <param name="request">HttpRequest Object</param>
        /// <returns></returns>
        public static string GetCookie(string name, HttpRequest request)
        {
            if (request.Cookies.Get(name) != null)
                return HttpUtility.UrlDecode(request.Cookies[name].Value);
            else
                return null;
        }
        /// <summary>
        /// 设置Cookie
        /// </summary>
        /// <param name="name">Cookie Name</param>
        /// <param name="value">设置的值</param>
        public static void SetCookie(string name, string value)
        {
            HttpResponse response = HttpContext.Current.Response;
            HttpRequest request = HttpContext.Current.Request;
            SetCookie(name, value, response, request, 1);
        }
        /// <summary>
        /// 设置Cookie,加过期时间
        /// </summary>
        /// <param name="name">Cookie Name</param>
        /// <param name="value">设置的值</param>
        /// <param name="hours">过期时间(小时)</param>
        public static void SetCookie(string name, string value, int hours)
        {
            HttpResponse response = HttpContext.Current.Response;
            HttpRequest request = HttpContext.Current.Request;
            SetCookie(name, value, response, request, hours);
        }
        /// <summary>
        /// 设置Cookie
        /// </summary>
        /// <param name="name">Cookie Name</param>
        /// <param name="value">设置的值</param>
        /// <param name="response">HttpResponse</param>
        /// <param name="request">HttpRequest</param>
        public static void SetCookie(string name, string value, HttpResponse response, HttpRequest request)
        {
            SetCookie(name, value, response, request, 1);
        }
        /// <summary>
        /// 设置Cookie
        /// </summary>
        /// <param name="name">Cookie Name</param>
        /// <param name="value">设置的值</param>
        /// <param name="response">HttpResponse</param>
        /// <param name="request">HttpRequest</param>
        /// <param name="hours">过期时间(小时)</param>
        public static void SetCookie(string name, string value, HttpResponse response, HttpRequest request, int hours)
        {
            HttpCookie MyCookie;

            //Response
            if (response.Cookies.Get(name) != null)
                MyCookie = response.Cookies[name];
            else
                MyCookie = new HttpCookie(name);
            DateTime now = DateTime.Now;
            MyCookie.Value = HttpUtility.UrlEncode(value);
            MyCookie.Expires = now.AddHours(hours);
            MyCookie.Path = request.ApplicationPath;
            if (response.Cookies.Get(name) == null)
                response.Cookies.Add(MyCookie);

            //Request			
            if (request.Cookies.Get(name) != null)
                MyCookie = request.Cookies[name];
            else
                MyCookie = new HttpCookie(name);
            MyCookie.Value = HttpUtility.UrlEncode(value);
            MyCookie.Expires = now.AddHours(hours);
            MyCookie.Path = request.ApplicationPath;
        }
        /// <summary>
        /// 删除Cookie
        /// </summary>
        /// <param name="name">Cookie Name</param>
        /// <param name="Request">HttpRequest</param>
        /// <param name="Response">HttpResponse</param>
        public static void RemoveCookie(string name, HttpRequest Request, HttpResponse Response)
        {
            if (Request.Cookies.Get(name) == null) return;
            Request.Cookies.Remove(name);
            if (Response.Cookies.Get(name) == null) return;
            Response.Cookies.Remove(name);
        }
        #endregion
    }
}
